package com.hengqin.macao.utils;

import android.util.Log;

import org.apache.commons.collections4.MapUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static java.lang.String.valueOf;

public class OkHttpUtil {

    private static final String TAG = OkHttpUtil.class.getSimpleName();

    private final static Long TIMEOUT = 30000L;

    private final static TimeUnit UNIT = TimeUnit.MILLISECONDS;

    private static OkHttpClient client = new OkHttpClient();

    public static Request postFile(String url,File file,Map<String, String> maps){
        MultipartBody.Builder builder=  new MultipartBody.Builder().setType(MultipartBody.FORM);
        if(maps==null){
            builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"file\";filename=\"file.jpg\""), RequestBody.create(MediaType.parse("image/png"),file)
            ).build();

        }else{
            for (String key : maps.keySet()) {
                builder.addFormDataPart(key, maps.get(key));
            }

            builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"file\";filename=\"file.jpg\""), RequestBody.create(MediaType.parse("image/png"),file)
            );

        }
        RequestBody body=builder.build();
        return new Request.Builder().url(url).post(body).build();
    }


    public static void postFile(final String url, final Map<String, Object> map, File file, Callback callback) {
        // form 表单形式上传
        MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
        if (file != null) {
            // MediaType.parse() 里面是上传的文件类型。
            RequestBody body = RequestBody.create(MediaType.parse("image/*"), file);
            String filename = file.getName();
            // 参数分别为， 请求key ，文件名称 ， RequestBody
            requestBody.addFormDataPart("avata", filename, body);
        }
        if (map != null) {
            // map 里面是请求中所需要的 key 和 value
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            for (Map.Entry entry : entries) {
                String key = valueOf(entry.getKey());
                String value = valueOf(entry.getValue());
                Log.d("HttpUtils", "key=="+key+"value=="+value);
                requestBody.addFormDataPart(key,value);
            }
        }
        Request request = new Request.Builder().url(url).post(requestBody.build()).build();
        // readTimeout("请求超时时间" , 时间单位);
        client.newBuilder().readTimeout(OkHttpUtil.TIMEOUT, OkHttpUtil.UNIT).build().newCall(request).enqueue(callback);

    }

    /**
     * Post请求
     * @param url
     * @param params 参数
     * @param callback 回调函数
     */
    public static void httpPost(String url,Map<String,String> params,final Callback callback) {
        //Log.i(TAG, "******httpPost url: " + url);
        if(callback != null){
            FormBody.Builder formBodyBuilder = new FormBody.Builder();
            if(MapUtils.isNotEmpty(params)){
                Set<String> keySet = params.keySet();
                for(String key:keySet) {
                    String value = params.get(key);
                    formBodyBuilder.add(key,value);
                }
            }
            FormBody formBody = formBodyBuilder.build();

            Request request = new Request
                    .Builder()
                    .post(formBody)
                    .url(url)
                    .build();
            client.newBuilder().readTimeout(OkHttpUtil.TIMEOUT, OkHttpUtil.UNIT).build().newCall(request).enqueue(callback);
        }
    }

}
